package com.common;

import java.util.UUID;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.vo.PageBean;

public class JPAUtil {
    public static boolean isNull(Object o) {
        return o == null ? true : false;
    }

    public static boolean isNull(String o) {
        return o == null || o.trim().equals("") ? true : false;
    }

    /**
     * 
     * @description: 获取分页PageBable对象
     * @param pageIndex
     *            页面索引 从1开始
     * @param pageSize
     *            页面大小，最大不超过100条
     * @param totalSize
     *            总页面
     * @param sort
     *            排序
     * @param pageResponse
     *            页面对象
     * @return
     * @author: zhangsike
     * @date: 2018/11/17 17:12:32
     */
    public static <T> Pageable getPageRequest(int pageIndex, int pageSize, long totalSize, Sort sort,
        PageBean<T> pageResponse) {
        if (pageSize > 1000)
            pageSize = 1000;
        if (pageIndex <= 1)
            pageIndex = 1;

        int totalPageNum = (int)Math.ceil(totalSize * 1.0 / pageSize);
        if (totalPageNum > 0 && pageIndex > totalPageNum)
            pageIndex = totalPageNum;

        pageResponse.setPageCount(totalPageNum);
        pageResponse.setPageIndex(pageIndex);
        Pageable request = PageRequest.of(pageIndex - 1, pageSize, sort);
        return request;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static Sort getDescSort(String k) {
        return new Sort(Direction.DESC, k);
    }

    public static Sort getAscSort(String k) {
        return new Sort(Direction.ASC, k);
    }
}
